package com.rockcent.service.impl;

import com.rockcent.common.common.BusinessException;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.common.utils.DateUtils;
import com.rockcent.mall.lib.domain.*;
import com.rockcent.mall.lib.domain.enumclass.MerchantStatus;
import com.rockcent.mall.lib.domain.enumclass.SystemType;
import com.rockcent.mall.lib.repository.*;
import com.rockcent.service.MerchantSrv;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * 商户信息处理
 * Created by zhangjh on 2017/3/29.
 */
@Service
public class MerchantSrvImpl implements MerchantSrv {

    @Autowired
    private MerchantRepository merchantRepository;

    @Autowired
    private MallFinancialSettingsRepository merchantFinancialSettingsRepository;

    @Autowired
    private UserMerchantRepository userMerchantRepository;

    @Autowired
    private AuthUserResourceDefaultRepository authUserResourceDefaultRepository;

    @Autowired
    private AuthUserResourceRepository authUserResourceRepository;


    @Override
    public void update(Merchant merchant) {

        if (null == merchant) {
            throw new BusinessException("商户数据为空");
        }

        if (null == merchant.getId()) {
            throw new BusinessException("商户ID为空");
        }

        if (null == merchant.getStatus()) {
            throw new BusinessException("商户状态为空");
        }

        merchantRepository.save(merchant);
    }


    /**
     * 设置商户试用期到期，设置商户服务费结算日等信息
     *
     * @param merchantInput
     */
    @Override
    public void updateOfProbation(Merchant merchantInput) {
        if (null == merchantInput) {
            throw new BusinessException("商户数据为空");
        }
        Long merchantId = merchantInput.getId();
        Long mallId = merchantInput.getMallId();
        if (null == merchantId) {
            throw new BusinessException("商户ID为空");
        }
        if (null == mallId) {
            throw new BusinessException("平台ID为空");
        }
        //商户信息
        Merchant merchant = merchantRepository.findOne(merchantId);

        //平台财务设置
        MallFinancialSettings merchantFinancialSettings = merchantFinancialSettingsRepository.findTop1ByMallIdAndIsDeleteOrderByLastUpdatedDesc(mallId, Whether.NO);
        Long probationDays = merchantFinancialSettings.getProbationDays();
        int probationDay = null == probationDays ? 0 : probationDays.intValue();
        Date dateEnabled = new Date();
        Date dateDisabled = DateUtils.getFutureDateByAddSomeDays(dateEnabled, probationDay);
        merchant.setDateEnabled(dateEnabled);
        merchant.setDateDisabled(dateDisabled);

        LocalDate today = LocalDate.now();

        //结算日
        String settlementDate = String.format("%02d", today.plusDays(1L + probationDay).getDayOfMonth());
        merchant.setSettlementDate(settlementDate);
        if (probationDay <= 0) {
            merchant.setStatus(MerchantStatus.NORMAL);
        } else {
            merchant.setStatus(MerchantStatus.PROBATION);
        }

        merchantRepository.save(merchant);

    }

    @Override
    public Merchant findOne(Long merchantId) {
        return merchantRepository.findOne(merchantId);
    }

    @Override
    public void saveUserMerchant(Merchant merchant, String userName) {
        // 创建商家权限账号
        UserMerchant userMerchant = userMerchantRepository.findByUserIdAndMallIdAndMerchantId(merchant.getUserInfoId(), merchant.getMallId(), merchant.getId());
        if (userMerchant == null) {
            userMerchant = new UserMerchant();
            userMerchant.setMallId(merchant.getMallId());
            userMerchant.setMerchantId(merchant.getId());
            userMerchant.setUserId(merchant.getUserInfoId());
            userMerchant.setIsSuperAdmin(Whether.YES);
            userMerchant.setUsername(userName);
            userMerchant.setAvailable(Whether.YES);
            userMerchant.setTitleName("超级管理员");
            userMerchant.setName(merchant.getName());
            userMerchantRepository.save(userMerchant);
        }

        //添加默认菜单
        List<AuthUserResourceDefault> authUserResourceDefaultList = authUserResourceDefaultRepository.findBySystemType(SystemType.MERCHANT);
        if (authUserResourceDefaultList != null && authUserResourceDefaultList.size() > 0) {
            for (AuthUserResourceDefault resourceDefault: authUserResourceDefaultList) {
                AuthUserResource authUserResource = new AuthUserResource();
                authUserResource.setUserId(merchant.getUserInfoId());
                authUserResource.setResourceId(resourceDefault.getResourceId());
                authUserResource.setResourceParentId(resourceDefault.getResourceParentId());
                authUserResource.setMallId(merchant.getMallId());
                authUserResourceRepository.save(authUserResource);
            }
        }
    }
}
